package cn.itcast.web.converter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/*
* 定义类型转换器，请求参数类型跟方法参数类型不同自动转换
* */
@Component
public class StringToDateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String source) {
        try {
            if(source !=null){
                return new SimpleDateFormat("yyyy-MM-dd").parse(source);
            }
            return null;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
